<p>There are two classes in the Java standard library that deal with iterations: <code>Iterable&lt;T&gt;</code> and <code>Iterator&lt;T&gt;</code>. An
<code>Iterable&lt;T&gt;</code> represents a data structure that can be the target of the "for-each loop" statement, and an
<code>Iterator&lt;T&gt;</code> represents the state of an ongoing traversal. An <code>Iterable&lt;T&gt;</code> is generally expected to support
multiple traversals.</p>
<p>An <code>Iterator&lt;T&gt;</code> that also implements <code>Iterable&lt;t&gt;</code> by returning itself as its <code>iterator()</code> will not
support multiple traversals since its state will be carried over.</p>
<p>This rule raises an issue when the <code>iterator()</code> method of a class implementing both <code>Iterable&lt;T&gt;</code> and
<code>Iterator&lt;t&gt;</code> returns <code>this</code>.</p>
<h2>Noncompliant Code Example</h2>
<pre>
class FooIterator implements Iterator&lt;Foo&gt;, Iterable&lt;Foo&gt; {
  private Foo[] seq;
  private int idx = 0;

  public boolean hasNext() {
    return idx &lt; seq.length;
  }

  public Foo next() {
    return seq[idx++];
  }

  public Iterator&lt;Foo&gt; iterator() {
    return this; // Noncompliant
  }
  // ...
}
</pre>
<h2>Compliant Solution</h2>
<pre>
class FooSequence implements Iterable&lt;Foo&gt; {
  private Foo[] seq;

  public Iterator&lt;Foo&gt; iterator() {
    return new Iterator&lt;Foo&gt;() {
      private int idx = 0;

      public boolean hasNext() {
        return idx &lt; seq.length;
      }

      public Foo next() {
        return seq[idx++];
      }
    };
  }
  // ...
}
</pre>

